package com.yr.ryjk.festival;

import java.util.Date;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.festival.entity.Festival;
import com.yr.ryjk.festival.service.IFestivalService;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.util.JSONUtil;
/**
 * 节日祝福
 * @author Rick
 *
 */
@Controller
@RequestMapping("/festival")
public class FestivalController {
	@Autowired
	private IFestivalService festivalService;

	/**
	 * 获取指定节日图片的详细信息
	 * @param id
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getDepartmentInfo(String id) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,festivalService.getFesById(id));
	}

	/**
	 * 添加节日图片
	 * @param fes
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Festival festival) {
		festivalService.add(festival);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	/**
	 * 删除节日图片
	 * @param id 记录id
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("id") @NotBlank String ids) {
		festivalService.delete(ids);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	/**
	 * 修改节日图片
	 * @param id 记录id
	 * @param type 类型
	 * @param picUrl  图片url
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@RequestParam("id") @NotBlank String id,
			@RequestParam("picUrl") @NotBlank String picUrl,
			@RequestParam("festivalName") @NotBlank String festivalName,
			@RequestParam("startDate") @NotBlank @DateTimeFormat(pattern="yyyy-MM-dd") Date startDate,
			@RequestParam("endDate") @NotBlank @DateTimeFormat(pattern="yyyy-MM-dd") Date endDate) {
		festivalService.modify(id, picUrl, festivalName,startDate,endDate);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}

	/**
	 * 分页获取数据
	 * @param pageNo 页码
	 * @param pageSize   每页数量
	 * @param type  广告类型
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(
			@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
		return JSONUtil.getJSON(ErrorType.SUCCESS,
				festivalService.getListOfPage(pageNo, pageSize));
	}

	/**
	 * app端分类型获取列表,不拦截
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getFitList() {
		return JSONUtil.getJSON(ErrorType.SUCCESS,festivalService.getFitList());
	}
}
